<?php
    class AlimentosController extends AppController {
        var $name = 'Alimentos';
        var $uses = array('Alimento');
        var $helpers = array('Javascript');
        var $components = array('AdminParameters');

        function index(){

            $gruposAlimento = $this->Alimento->GrupoAlimento->find("all");
            $this->set("gruposAlimento", $gruposAlimento);

            $columnasCategoria = $this->Alimento->getPrimerAlimentoGrupo();
            $this->set("columnasCategoria", $columnasCategoria);

            $this->render("index");
        }

        function alimentos(){
            $this->render("alimentos", "ajax");
        }

        function  getList(){

            $this->render("list", "ajax");
        }

        function getListData($page=1, $limit=10, $orderColumn='fecha', $ascDesc='desc'){

            $model = "Alimento.";

            $fields = array(
                 $model."id", $model."numero", $model."alimento", $model."energia_kj", $model."energia_kcal",
                 $model."agua_g", $model."proteinas_g", $model."grasa_total_g",
                 $model."carbohidratos_total_g", $model."carbohidratos_disponible_g",
                 $model."fibra_g", $model."cenizas_g", $model."sodio_mg", $model."potasio_mg",
                 $model."calcio_mg", $model."fosforo_mg", $model."hierro_mg", $model."zinc_mg",
                 $model."tiamina_mg", $model."rivoflavina_mg", $model."niacina_mg", $model."vitamina_c_mg",
                 $model."ac_gr_saturados_g", $model."ac_gr_monoinsaturados_g", $model."ac_gr_poliinsaturados_g",
                 $model."colesterol_mg", $model."AGPw6", $model."AGPW3"
            );

            $result = $this->AdminParameters->getListData($this->Alimento, $fields, $this->params["form"], $page, $limit, $orderColumn, $ascDesc);

            $this->set("result", $result);

            $this->render("/elements/ajax_return", "ajax");

        }

        function getDetail($op, $id){

            $this->set("operation", $op);
            $this->set("editId", $id);

            $this->render("detalle_riesgos", "ajax");

        }

        function getDetailData($id){
            $data = $this->Alimento->find("all", array("conditions" => array("Alimento.id" => $id), "recursive" => 0));

            $this->set("result", array("data" => $data));

            $this->render("/elements/ajax_return", "ajax");
        }

        function save(){

            if($this->params["form"]["op"] == "add"){
                $numero = $this->Alimento->find('first', array('order' => array('Alimento.numero DESC')));
                $numero = $numero['Alimento']['numero'] + 1;

                $this->params["form"]["numero"] = $numero;
            }

            if($this->AdminParameters->doSave($this->Alimento, $this->params["form"])){
                $this->set("result", array("result" => true));
            }else{
                $this->set("result", array("result" => false));
                $this->set("result", array("error" => "Error while saving data."));
            }


            $this->render("/elements/ajax_return", "ajax");
        }

        function getAlimentos($filter){
            
            /* "fields" => array("Alimento.id", "Alimento.alimento",
                            "Alimento.carbohidratos_total_g",  "Alimento.proteinas_g",
                            "Alimento.carbohidratos_disponible_g", 
                            "Alimento.energia_kcal", "Alimento.grasa_total_g",
                            "Alimento.ajuste_g_cal"),
             */

            $data = $this->Alimento->find(
                    "all",
                    array(
                            "conditions" => array(
                                "Alimento.alimento LIKE" => "%".$filter."%"
                            ),
                            "order" => array("Alimento.alimento ASC")
                        )
             );

            $this->set("result", $data);

            $this->render("/elements/ajax_return", "ajax");
        }
    }
?>